<?php

/**
 *
 * @author Nguyen Thai Binh
 */

function set_userdata($key, $value) {
  if (!empty($key) && !empty($key)) {
    $ci = & get_instance();
    $ci->session->set_userdata($ci->config->item('project_name').'_'.$key, $value);
  }
}

function unset_userdata($key) {
  if (isset($key) && !empty($key)) {
    $ci = & get_instance();
    $ci->session->unset_userdata($ci->config->item('project_name').'_'.$key);
  }
}

function userdata($key, $default = '') {
  if (!empty($key)) {
    $ci = & get_instance();
    $value = $ci->session->userdata($ci->config->item('project_name').'_'.$key);
    return empty($value) ? $default : $value;
  } else
    return $default;
}

function has_message() {
  return userdata('message') != '';
}
function flash_message($tag = 'p') {
  $flashmsg = userdata('message');
  $html = '';
  if (is_array($flashmsg)) {
    foreach ($flashmsg as $value) {
      $html .= "<$tag>" . $value . "</$tag>";
    }
  } else
    $html = $flashmsg;
  unset_userdata('message');
  return $html;
}

function set_flash_message($message) {
  set_userdata('message', $message);
}

function has_attention() {
  return userdata('attention') != '';
}
function flash_attention($tag = 'p') {
  $flashmsg = userdata('attention');
  $html = '';
  if (is_array($flashmsg)) {
    foreach ($flashmsg as $value) {
      $html .= "<$tag>" . $value . "</$tag>";
    }
  } else
    $html = $flashmsg;
  unset_userdata('attention');
  return $html;
}

function set_flash_attention($message) {
  set_userdata('attention', $message);
}

function has_information() {
  return userdata('information') != '';
}
function flash_information($tag = 'p') {
  $flashmsg = userdata('attention');
  $html = '';
  if (is_array($flashmsg)) {
    foreach ($flashmsg as $value) {
      $html .= "<$tag>" . $value . "</$tag>";
    }
  } else
    $html = $flashmsg;
  unset_userdata('information');
  return $html;
}

function set_flash_information($message) {
  set_userdata('information', $message);
}

function set_flash_error($message) {
  set_userdata('error', $message);
}

function has_error() {
  return userdata('error') != '';
}

function flash_error($tag = 'p') {
  $flashmsg = userdata('error');
  $html = '';
  if (is_array($flashmsg)) {
    foreach ($flashmsg as $value) {
      $html .= "<$tag>" . $value . "</$tag>";
    }
  } else
    $html = $flashmsg;
  unset_userdata('error');
  return $html;
}